#!/usr/bin/env python
'''MainWindow

this class handles the main window
'''

import gtk
import Brands
import Breweries
import Coasters
import Tools
import os
import sys
from Config import get_config
from FirstTime import FirstTime
from GladeWidgets import GladeWidget

class MainWindow(gtk.Window):
    '''Display the main window'''

    def __init__(self):
        '''
        Here we setup the glade environment and show the window
        '''

        first = FirstTime()

        self.wndMain = GladeWidget("wndMain")
        self.wndMain["wndMain"].set_title(get_config("title"))
        self.wndMain["wndMain"].set_icon_from_file(get_config("icon_path"))

        # setup signals
        self._setupSignals()

        # setup the model for the treeviews
        Breweries.setTreeModel(self.wndMain)
        Breweries.showTreeview(self.wndMain["treeviewBreweries"])
        Brands.setTreeModel(self.wndMain)
        Brands.showTreeview(self.wndMain["treeviewBrands"])
        Coasters.setTreeModel(self.wndMain)
        Coasters.showTreeview(self.wndMain["treeviewCoasters"])
        self.wndMain["wndMain"].show()

    def on_btnAddBrewery_clicked(self, event):
        '''Add a new brewery (wrapper)'''
        Breweries.on_btnAddBrewery_clicked(self.wndMain, event)

    def on_btnEditBrewery_clicked(self, event):
        '''Edit an existing brewery'''
        Breweries.on_btnEditBrewery_clicked(self.wndMain, event)

    def on_btnDelBrewery_clicked(self, event):
        '''Delete an existing brewery'''
        Breweries.on_btnDelBrewery_clicked(self.wndMain, event)

    def on_txtFilterBreweries_changed(self, event):
        '''Update the treeview according to the brewery's filter'''
        text = self.wndMain["txtBreweryFilter"].get_text()
        Breweries.showTreeview(self.wndMain["treeviewBreweries"], "%" + text + "%")

    def on_btnBrandAdd_clicked(self, event):
        '''Add a new brand (wrapper)'''
        Brands.on_btnBrandAdd_clicked(self.wndMain, event)

    def on_btnBrandDel_clicked(self, event):
        '''Delete the selected brand'''
        Brands.on_btnBrandDel_clicked(self.wndMain, event)

    def on_btnBrandEdit_clicked(self, event):
        '''Show the edit brand's window'''
        Brands.on_btnBrandEdit_clicked(self.wndMain, event)

    def on_txtBrandFilter_changed(self, event):
        '''When something gets written into the filter, update the treeview'''
        text = self.wndMain["txtBrandFilter"].get_text()
        Brands.showTreeview(self.wndMain["treeviewBrands"], "%" + text + "%")

    def on_btnCoasterAdd_clicked(self, event):
        '''Show the add coaster's window'''
        Coasters.on_btnCoasterAdd_clicked(self.wndMain, event)

    def on_btnCoasterEdit_clicked(self, event):
        '''Show the edit coaster's window'''
        Coasters.on_btnCoasterEdit_clicked(self.wndMain, event)

    def on_btnCoasterDel_clicked(self, event):
        '''Delete the selected coaster'''
        Coasters.on_btnCoasterDel_clicked(self.wndMain, event)

    def on_btnDetails_clicked(self, event):
        '''Show the details coaster's window'''
        Coasters.on_btnDetails_clicked(self.wndMain, event)

    def on_txtCoasterFilter_changed(self, event):
        '''When something gets written into the coaster filter, update the treeview'''
        text = self.wndMain["txtCoasterFilter"].get_text()
        Coasters.showTreeview(self.wndMain["treeviewCoasters"], "%" + text + "%")

    def on_btnExport_clicked(self, event):
        '''Export the coasters in an HTML page'''
        Tools.on_btnExport_clicked(self.wndMain, event)

    def _setupSignals(self):
        '''Set up the signals for the main window'''
        sig = {
                'on_btnAddBrewery_clicked':         self.on_btnAddBrewery_clicked,
                'on_btnEditBrewery_clicked':        self.on_btnEditBrewery_clicked,
                'on_btnDelBrewery_clicked':         self.on_btnDelBrewery_clicked,
                'on_txtFilterBreweries_changed':    self.on_txtFilterBreweries_changed,
                'on_btnBrandAdd_clicked':           self.on_btnBrandAdd_clicked,
                'on_btnBrandEdit_clicked':          self.on_btnBrandEdit_clicked,
                'on_btnBrandDel_clicked':           self.on_btnBrandDel_clicked,
                'on_txtBrandFilter_changed':        self.on_txtBrandFilter_changed,
                'on_btnCoasterAdd_clicked':         self.on_btnCoasterAdd_clicked,
                'on_btnCoasterEdit_clicked':        self.on_btnCoasterEdit_clicked,
                'on_btnCoasterDel_clicked':         self.on_btnCoasterDel_clicked,
                'on_btnDetails_clicked':            self.on_btnDetails_clicked,
                'on_txtCoasterFilter_changed':      self.on_txtCoasterFilter_changed,
                'on_btnExport_clicked':             self.on_btnExport_clicked,
                'on_wndMain_destroy':               self.on_wndMain_destroy,
        }

        self.wndMain.widgets.signal_autoconnect(sig)

    def on_wndMain_destroy(self, event):
        '''Close the window'''
        gtk.main_quit()
